package com.anxin.converter;

import com.anxin.entity.Appointment;
import com.anxin.entity.excel.AppointmentExcel;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class AppointmentConverter {
    
    public AppointmentExcel toExcel(Appointment appointment) {
        if (appointment == null) {
            return null;
        }
        
        AppointmentExcel excel = new AppointmentExcel();
        excel.setId(appointment.getId());
        excel.setPatientId(appointment.getPatientId());
        excel.setDoctorId(appointment.getDoctorId());
        excel.setDepartmentId(appointment.getDepartmentId());
        excel.setScheduleId(appointment.getScheduleId());
        excel.setAppointmentDate(Date.from(appointment.getAppointmentDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        excel.setAppointmentTime(getAppointmentTimeText(appointment.getAppointmentTime()));
        excel.setAppointmentNumber(appointment.getAppointmentNumber());
        excel.setVisitNumber(appointment.getVisitNumber());
        excel.setRegistrationFee(appointment.getRegistrationFee().doubleValue());
        excel.setStatus(getStatusText(appointment.getStatus()));
        excel.setCancelReason(appointment.getCancelReason());
        excel.setCreateTime(Date.from(appointment.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
        excel.setUpdateTime(appointment.getUpdateTime() != null ? 
            Date.from(appointment.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant()) : null);
        
        return excel;
    }
    
    public Appointment toEntity(AppointmentExcel excel) {
        if (excel == null) {
            return null;
        }
        
        Appointment appointment = new Appointment();
        appointment.setId(excel.getId());
        appointment.setPatientId(excel.getPatientId());
        appointment.setDoctorId(excel.getDoctorId());
        appointment.setDepartmentId(excel.getDepartmentId());
        appointment.setScheduleId(excel.getScheduleId());
        appointment.setAppointmentDate(excel.getAppointmentDate().toInstant()
            .atZone(ZoneId.systemDefault()).toLocalDate());
        appointment.setAppointmentTime(getAppointmentTimeValue(excel.getAppointmentTime()));
        appointment.setAppointmentNumber(excel.getAppointmentNumber());
        appointment.setVisitNumber(excel.getVisitNumber());
        appointment.setRegistrationFee(BigDecimal.valueOf(excel.getRegistrationFee()));
        appointment.setStatus(getStatusValue(excel.getStatus()));
        appointment.setCancelReason(excel.getCancelReason());
        appointment.setCreateTime(excel.getCreateTime().toInstant()
            .atZone(ZoneId.systemDefault()).toLocalDateTime());
        appointment.setUpdateTime(excel.getUpdateTime() != null ? 
            excel.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() : null);
        
        return appointment;
    }
    
    public List<AppointmentExcel> toExcelList(List<Appointment> appointmentList) {
        if (appointmentList == null) {
            return null;
        }
        
        return appointmentList.stream()
                .map(this::toExcel)
                .collect(Collectors.toList());
    }
    
    public List<Appointment> toEntityList(List<AppointmentExcel> excelList) {
        if (excelList == null) {
            return null;
        }
        
        return excelList.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }
    
    private String getAppointmentTimeText(Integer appointmentTime) {
        if (appointmentTime == null) {
            return null;
        }
        return appointmentTime == 1 ? "上午" : "下午";
    }
    
    private Integer getAppointmentTimeValue(String appointmentTime) {
        if (appointmentTime == null) {
            return null;
        }
        return appointmentTime.equals("上午") ? 1 : 2;
    }
    
    private String getStatusText(Integer status) {
        if (status == null) {
            return null;
        }
        switch (status) {
            case 1:
                return "待就诊";
            case 2:
                return "已就诊";
            case 3:
                return "已取消";
            case 4:
                return "已过期";
            default:
                return "未知";
        }
    }
    
    private Integer getStatusValue(String status) {
        if (status == null) {
            return null;
        }
        switch (status) {
            case "待就诊":
                return 1;
            case "已就诊":
                return 2;
            case "已取消":
                return 3;
            case "已过期":
                return 4;
            default:
                return null;
        }
    }
} 